package com.chase.framerwork.excel;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.NumberToTextConverter;

/**
 * Excel Bean
 * 
 * @Author Chase
 * @Date 2013-9-26
 */
public class Excel
{
	/**
	 * 文件名
	 */
	private String fileName;

	/**
	 * Excel工作薄, HSSF,XSSF的父类
	 */
	private Workbook workbook;

	/**
	 * Sheet页，Excel工作表
	 */
	private Sheet sheet;

	/**
	 * Sheet页名称
	 */
	private String sheetName;

	/**
	 * Excel文件类型
	 */
	private ExcelFileType excelFileType;

	/**
	 * 从Sheet中获取第几行
	 * 
	 * @param num
	 * @return
	 */
	public Row getRow(int num)
	{
		return sheet.getRow(num);
	}

	/**
	 * 从Sheet中获取指定行和列的单元格
	 * 
	 * @param num
	 * @param col
	 * @return
	 */
	public Cell getCell(int num, int col)
	{
		Row row = getRow(num);
		return row.getCell(col);
	}

	/**
	 * 从Sheet中获取指定行和列的单元格的值
	 */
	public String getCellValue(int num, int col)
	{
		Cell cell = getCell(num, col);
		return getValue(cell);
	}

	/**
	 * 得到Sheet中的总行数
	 * 
	 * @return
	 */
	public int getRowCount()
	{
		return sheet.getLastRowNum();
	}

	/**
	 * 得到Sheet中某一行的列总数
	 * 
	 * @param rownum
	 * @return
	 */
	public int getColCount(int rownum)
	{
		return sheet.getRow(rownum).getPhysicalNumberOfCells();
	}

	/**
	 * 从Sheet中创建一行
	 * 
	 * @param rownum
	 * @return
	 */
	public HSSFRow createRow(int rownum)
	{
		return (HSSFRow) sheet.createRow(rownum);
	}

	/**
	 * 得到Cell的Value
	 * 
	 * @param cell
	 * @return
	 */
	public String getValue(Cell cell)
	{
		switch (cell.getCellType())
		{
		case Cell.CELL_TYPE_BLANK:
			return "";
		case Cell.CELL_TYPE_BOOLEAN:
			return String.valueOf(cell.getBooleanCellValue());
		case Cell.CELL_TYPE_ERROR:
			break;
		case Cell.CELL_TYPE_FORMULA:
			return cell.getCellFormula();
		case Cell.CELL_TYPE_NUMERIC:
			return NumberToTextConverter.toText(cell.getNumericCellValue());
		case Cell.CELL_TYPE_STRING:
			return cell.getStringCellValue();
		default:
			return cell.getStringCellValue();
		}
		return cell.getStringCellValue();
	}

	public String getFileName()
	{
		return fileName;
	}

	public void setFileName(String fileName)
	{
		this.fileName = fileName;
	}

	public Workbook getWorkbook()
	{
		return workbook;
	}

	public void setWorkbook(Workbook workbook)
	{
		this.workbook = workbook;
	}

	public Sheet getSheet()
	{
		return sheet;
	}

	public void setSheet(Sheet sheet)
	{
		this.sheet = sheet;
	}

	public ExcelFileType getExcelFileType()
	{
		return excelFileType;
	}

	public void setExcelFileType(ExcelFileType excelFileType)
	{
		this.excelFileType = excelFileType;
	}

	public String getSheetName()
	{
		return sheetName;
	}

	public void setSheetName(String sheetName)
	{
		this.sheetName = sheetName;
	}
}
